package dtd.PHS.VND_ExchangeRate.ui;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;

public abstract class InOutAnimation {
	
	protected Handler handler;
	private Context context;
	
	public InOutAnimation(Handler handler,Context context) {
		this.handler = handler;
		this.context = context;
	}

	public void startAnimations(
			final View currentView,
			final View nextView,
			int slideOutAnim, 
			final int slideInAnim) 
	{
		Animation outAnimation = AnimationUtils.loadAnimation(context, slideOutAnim);
		outAnimation.setFillAfter(false);
		
		outAnimation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
				handler.post(new Runnable() {
					public void run() {
						currentView.setVisibility(View.GONE);
					}
				});
				
			}
			
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				handler.post(new Runnable() {
					public void run() {
						Animation inAnimation = createInAnimation();
						nextView.startAnimation(inAnimation);
					}

					private Animation createInAnimation() {
						Animation inAnimation = AnimationUtils.loadAnimation(context,slideInAnim);
						inAnimation.setFillAfter(false);
						inAnimation.setAnimationListener(new AnimationListener() {
							public void onAnimationStart(Animation animation) {}
							public void onAnimationRepeat(Animation animation) {}
							@Override
							public void onAnimationEnd(Animation animation) {
								handler.post(new Runnable() {
									@Override
									public void run() {
										nextView.setVisibility(View.VISIBLE);
										onNewViewDisplayed();
									}
								});
							}
						});
						return inAnimation;
					}


				});

								
			}
		});
		currentView.startAnimation(outAnimation);
	}
	public abstract void onNewViewDisplayed(); 
//	{
//		currentTabId = newTabId;
//		Helpers.setPreference(Helpers.PREF_LAST_TAB_ID, ""+currentTabId);
//		requestRates();
//	}

}
